using System;
using System.Collections.Generic;
//using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web;
using System.Web.Http;
using LynxBlogAPI.Models;

namespace LynxBlogAPI.Controllers
{
    public class PageDataController : ApiController
    {
        private LynxWebLog_Context db = new LynxWebLog_Context();

        // GET api/PageData
        [Queryable]
        public IEnumerable<PageData> GetPageData()
        {
            return db.PageDatas.AsEnumerable();
        }

        // GET api/PageData/5
        [Queryable]
        public PageData GetPageData(Int32 id)
        {
            PageData pagedata = db.PageDatas.Find(id);
            if (pagedata == null)
            {
                throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
            }

            return pagedata;
        }

        // PUT api/PageData/5
        public HttpResponseMessage PutPageData(Int32 id, PageData pagedata)
        {
            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != pagedata.pageid)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(pagedata).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }

        // POST api/PageData
        public HttpResponseMessage PostPageData(PageData pagedata)
        {
            if (ModelState.IsValid)
            {
                db.PageDatas.Add(pagedata);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, pagedata);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = pagedata.pageid }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }

        // DELETE api/PageData/5
        public HttpResponseMessage DeletePageData(Int32 id)
        {
            PageData pagedata = db.PageDatas.Find(id);
            if (pagedata == null)
            {
                return Request.CreateResponse(HttpStatusCode.NotFound);
            }

            db.PageDatas.Remove(pagedata);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK, pagedata);
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}